﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Ultima.Xna.Input;
using System.Xml;

namespace Ultima.Xna.Graphics.UI
{
    public class GumpButton : UINode, IGumpButton
    {
        private int _normalId;
        private int _pressedId;
        private int _buttonId;
        private int _param;

        private bool _pressed;

        private Texture2D _normalTexture;
        private Texture2D _pressedTexture;

        private Color[] _normalTextureData;
        private Color[] _pressedTextureData;

        public bool Pressed
        {
            get { return _pressed; }
            set { _pressed = value; }
        }

        public Texture2D NormalTexture
        {
            get { return _normalTexture; }
        }

        public Texture2D PressedTexture
        {
            get { return _pressedTexture; }
        }

        public int NormalId
        {
            get { return _normalId; }
            set { _normalId = value; }
        }

        public int PressedId
        {
            get { return _pressedId; }
            set { _pressedId = value; }
        }

        public int ButtonId
        {
            get { return _buttonId; }
            set { _buttonId = value; }
        }

        public int Param
        {
            get { return _param; }
            set { _param = value; }
        }

        public override Vector2 Size
        {
            get
            {
                if (_pressed)
                {
                    if (_pressedTexture != null)
                    {
                        return new Vector2(_pressedTexture.Width, _pressedTexture.Height);
                    }
                    else
                    {
                        return base.Size;
                    }
                }
                else
                {
                    if (_normalTexture != null)
                    {
                        return new Vector2(_normalTexture.Width, _normalTexture.Height);
                    }
                    else
                    {
                        return base.Size;
                    }
                }
            }
            set
            {
                base.Size = value;
            }
        }

        public GumpButton(Game game, IUIContainerNode parent)
            : base(game, parent)
        {

        }

        public override void Initialize(XmlElement element)
        {
            base.Initialize(element);

            _normalTexture = Data.Gumps.CreateTexture(_game.GraphicsDevice, _normalId);
            _pressedTexture = Data.Gumps.CreateTexture(_game.GraphicsDevice, _pressedId);

            _normalTextureData = new Color[_normalTexture.Width * _normalTexture.Height];
            _pressedTextureData = new Color[_pressedTexture.Width * _pressedTexture.Height];

            _normalTexture.GetData<Color>(_normalTextureData);
            _pressedTexture.GetData<Color>(_pressedTextureData);
        }

        public override void HandleClick(MouseButtons button, bool down)
        {
            if (_defaultBehaviour)
            {
                _pressed = down;

                if (!down)
                {
                    base.HandleClick(button, down);
                }
            }
            else
            {
                base.HandleClick(button, down);
            }
        }

        public override void HandleMouseLeave()
        {
            if (_defaultBehaviour)
            {
                _pressed = false;
            }
            else
            {
                base.HandleMouseLeave();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            RectangleF renderBounds;
            PresentationParameters pp = _graphicsDevice.PresentationParameters;

            CalculateRenderBounds(out renderBounds);

            Texture2D texture = _normalTexture;

            if (_pressed)
                texture = _pressedTexture;

            _spriteBatch.Draw(texture, renderBounds, Color.White);

            base.Draw(gameTime);
        }

        public override void HitTest(ref float x, ref float y, out bool value)
        {
            base.HitTest(ref x, ref y, out value);

            if (value)
            {
                RectangleF renderBounds;
                CalculateRenderBounds(out renderBounds);

                float cx = x - renderBounds.X;
                float cy = y - renderBounds.Y;

                if (_pressed)
                {
                    value = _normalTextureData[(int)cx + ((int)cy * _normalTexture.Width)].A > 0;
                }
                else
                {
                    value = _pressedTextureData[(int)cx + ((int)cy * _pressedTexture.Width)].A > 0;
                }
            }
        }
    }
}
